﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RainEngine.Controllers;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 水平滑动条
    /// </summary>
    public class UIHSlider:UISliderBase  
    {
        /// <summary>
        /// 创建一个默认水平滑动条
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        public UIHSlider(RainGE engine, Scene setScene)
            : base(engine, setScene, Vector2.Zero, "UIDxutcontrols", DefaultSliderRailSize, DefaultSliderRailRect, DefaultSliderButtonSize, DefaultSliderButtonRect, DefaultSliderButtonRect, DefaultSliderButtonRect,true)
        {
            //设置水平滑动条滑块的位置
            this.sliderButtonPosition = new Vector2(- sliderButtonSize.X / 2, 0);        
        }

        public override void Update(GameTime gameTime)
        {

            //如果鼠标在滑动条上且左键按下           
            if (isMouseInside && Input.MouseLeftButtonPressed)
            {
                isSliding = true;

                float value = 0;
                //计算滑动条的值                    
                value = minValue + ((int)(System.Math.Round((Input.MousePos.X - Position.X) * tickFrequency / sliderRailSize.X)) * (maxValue - minValue) / tickFrequency);
                //不让value值超出范围
                if (value < minValue)
                    value = minValue;
                else if (value > maxValue)
                    value = maxValue;

                //如果值变化则重新设置新值并引发ValueChanged事件
                if (this.value != value)
                {
                    this.value = value;
                    OnValueChanged();
                }

                else
                    isSliding = false;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// 绘制滑动条
        /// </summary>
        public override int Draw(GameTime gameTime)
        {

            // 获取图像淡入淡出的透明颜色
            alphaTextureColor = new Color(Color.R,Color .G,Color.B, Scene.TransitionAlpha);

            //绘制滑动条轨道轨道
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)(Position.Y + size.Y / 2 - sliderRailSize.Y / 2), (int)sliderRailSize.X, (int)sliderRailSize.Y), sliderRailRect, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);

            //根据鼠标移入还是按下设置滑块图像源矩阵                
            Rectangle buttonRect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed && isMouseInside) ? sliderButtonRectClicked : sliderButtonRectHighlight) : sliderButtonRectOrigin;
            //绘制滑块
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)sliderButtonPosition.X, (int)Position.Y, (int)sliderButtonSize.X, (int)sliderButtonSize.Y), buttonRect, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);
            return 1;
        }

        #region 事件相关处理程序        

        /// <summary>
        /// 重置水平轨道上滑块的位置
        /// </summary>
        protected override  void ResetSliderButtonPosition()
        {
            //根据value值重置按钮的位置
            sliderButtonPosition.X = (this.value - minValue) * (sliderRailSize.X) / (maxValue - minValue) + Position.X - sliderButtonSize.X / 2;
            
            //不让按钮超出滑动条边界
            if (sliderButtonPosition.X < Position.X - sliderButtonSize.X / 2)
                sliderButtonPosition.X = Position.X - sliderButtonSize.X / 2;
            else if (sliderButtonPosition.X > Position.X + sliderRailSize.X - sliderButtonSize.X / 2)
                sliderButtonPosition.X = Position.X + sliderRailSize.X - sliderButtonSize.X / 2; 
        }        

        /// <summary>
        /// 设置水平滑动条大小，水平滑动条的长为滑动条轨道的长，高为滑块的高
        /// </summary>
        protected override void ResetSize()
        {
           this.size = new Vector2(sliderRailSize.X, sliderButtonSize.Y);
        }

        #endregion

        
    }
}
